package android.GameLogic;

import java.util.Random;

/**
 * Created by IntelliJ IDEA.
 * User: akuryou
 * Date: 4/19/11
 * Time: 10:45 PM
 * To change this template use File | Settings | File Templates.
 */
public class GameLogic
{
    private Random moveGen;
    public GameLogic() {}

    /**
     * checks if a player manages to get n in a row to win.
     * @param playerId : the last player to place a symbol
     * @param gameType : 3-, 4-, or 5 in a row
     * @param array : the int array that contains the playerIDs
     * @return true if playerID got n in a row. False if not.
     */

    public boolean playerWon( int[][] array,  int x, int y, int playerId, int gameType )
    {
        boolean winner = false;

        if( checkRow( array, x, y, playerId, gameType ) ||
            checkColumn( array, x , y, playerId, gameType ) ||
            checkRightDiag(array, x, y, playerId, gameType) ||
            checkLeftDiag( array, x, y, playerId, gameType) ) winner = true;

        return winner;
    }

    /**
     * Common for all the checking methods:
     * 1: starts in the clicked cell
     * 2: search -> 0 if other cells contains the same playerid, and sets startx/starty to that cell
     * 3: starts in startx/starty cell and searches -> array.length, and counts the amount of times the playerid is in a row
     * 4: if in a row exceeds or is the same as gameType, it returns true
     */


    public boolean checkRow( int[][] array,  int x, int y, int playerId, int gameType )
    {
        int inARow = 0;
        boolean winner = false;
        int startX = 0;

        while( array[x][y] == playerId && x >= 0 )
        {
            startX = x;
            x--;
            if( x < 0 ) break;
        }

        while( array[startX][y] == playerId && startX < array.length )
        {
            inARow++;
            startX++;
            if( startX >= array.length )
            {
                if( inARow >= gameType ) winner = true;
                break;
            }
            if( inARow >= gameType )
            {
                winner = true;
            }
        }
        return winner;
    }

    public boolean checkColumn( int[][] array,  int x, int y, int playerId, int gameType )
    {
        int inARow = 0;
        boolean winner = false;
        int startY = 0;

        while( array[x][y] == playerId && y >= 0 )
        {
            startY = y;
            y--;
            if( y < 0 ) break;
        }

        while( array[x][startY] == playerId && startY < array[0].length )
        {
            inARow++;
            startY++;
            if( startY >= array.length )
            {
                if( inARow >= gameType ) winner = true;
                break;
            }
            if( inARow >= gameType )
            {
                winner = true;
            }
        }
        return winner;
    }

    public boolean checkRightDiag( int[][] array,  int x, int y, int playerId, int gameType )
    {
        int inARow = 0;
        boolean winner = false;
        int startX = 0;
        int startY = 0;

        while( array[x][y] == playerId && y < array[0].length )
        {
            startX = x;
            startY = y;
            x--;
            y++;
            if( y >= array[0].length || x < 0 ) break;
        }

        while( array[startX][startY] == playerId && startX >= 0 && startY < array.length )
        {
            inARow++;
            startX++;
            startY--;
            if( startX >= array.length || startY < 0)
            {
                if( inARow >= gameType ) winner = true;
                break;
            }
            if( inARow >= gameType )
            {
                winner = true;
            }
        }
        return winner;
    }

    public boolean checkLeftDiag( int[][] array,  int x, int y, int playerId, int gameType )
    {
        int inARow = 0;
        boolean winner = false;
        int startX = 0;
        int startY = 0;


        while( array[x][y] == playerId && y < array[0].length )
        {
            if( x == 0 && y == array[0].length - 1) break;
            startX = x;
            startY = y;
            x--;
            y--;
            if( x < 0 ) break;
            if( y < 0 ) break;
        }

        while( array[startX][startY] == playerId && startX < array.length && startY < array[0].length )
        {
            if( x == 0 && y == array[0].length - 1) break;
            inARow++;

            startX++;
            startY++;
            if( startX >= array.length )
            {
                if( inARow >= gameType ) winner = true;
                break;
            }
            if(  startY >= array[0].length )
            {
                if( inARow >= gameType ) winner = true;
                break;
            }
            if( inARow >= gameType )
            {
                winner = true;
            }
        }
        return winner;
    }

    public int[] AIMove( int array[][], int gameType )
    {
        int freeSpaces = 0;
        int[] move = new int[2];

        for( int i = 0; i < array.length; ++i )
        {
            for( int j = 0; j < array[0].length; ++j )
            {
                if( array[i][j] == 0)
                {
                    freeSpaces++;
                }
            }
        }
        if( freeSpaces > 0 )
        {
            int x = moveGen.nextInt(gameType);
            int y = moveGen.nextInt(gameType);

            if( array[x][y] == 0 )
            {
                move[0] = x;
                move[1] = y;
            }
        }
        return move;
    }
}